package propertytype

import (
	"reflect"
)

//Value 值
type Value struct {
	TID        string      `json:"tid" redis:"tid"`
	PID        string      `json:"pid" redis:"pid"`
	SubID      string      `json:"sub_id" redis:"sub_id"`
	Mindex     int         `json:"mindex" redis:"mindex"`
	Value      interface{} `json:"v" redis:"v"`
	ReportTime int64       `json:"rt" redis:"rt"`
	PickTime   int64       `json:"pt" redis:"pt"`
}

//Data 存储到数据库中的数据
type Data struct {
	Seq int64 `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"-"`
	Value
}

//StructValue 结构
type StructValue struct {
	SubID string
	Value interface{}
}

//DataArray 上报数组
type DataArray struct {
	TID   string `json:"tid" redis:"tid"`
	PID   string `json:"pid" redis:"pid"`
	SubID string `json:"sub_id" redis:"sub_id"`
	//Mindex     int           `json:"mindex" redis:"mindex"`
	Value      []interface{} `json:"v"`
	ReportTime int64         `json:"rt" redis:"rt"`
	PickTime   int64         `json:"pt" redis:"pt"`
}

//DataStruct 上报结构数据
type DataStruct struct {
	TID string `json:"tid" redis:"tid"`
	PID string `json:"pid" redis:"pid"`
	//SubID string `json:"sub_id" redis:"sub_id"`
	//Mindex     int         `json:"mindex" redis:"mindex"`
	Value      []StructValue `json:"v"`
	ReportTime int64         `json:"rt" redis:"rt"`
	PickTime   int64         `json:"pt" redis:"pt"`
}

//DataArrayStruct 上报结构数组
type DataArrayStruct struct {
	TID string `json:"tid" redis:"tid"`
	PID string `json:"pid" redis:"pid"`
	//SubID string `json:"sub_id" redis:"sub_id"`
	//Mindex     int           `json:"mindex" redis:"mindex"`
	Value      [][]StructValue `json:"v"`
	ReportTime int64           `json:"rt" redis:"rt"`
	PickTime   int64           `json:"pt" redis:"pt"`
}

func fmtStruct(item reflect.Value) interface{} {
	result := make([]StructValue, item.NumField())
	//item = reflect.Indirect(item)
	//result[i] = itemstruct
	//valuei := reflect.ValueOf(item)
	valuet := item.Type()
	num := item.NumField()
	for j := 0; j < num; j++ {
		//var field reflect.StructField
		//field = valuet.Field(j)
		filed := item.Field(j)
		//vfield := valuei.Field(j)
		fieldName := valuet.Field(j).Tag.Get("json")
		var iv interface{}
		if filed.CanInterface() {
			iv = filed.Interface()
			result[j] = StructValue{
				SubID: fieldName,
				Value: iv,
			}
		}
		//fmt.Println("kind---------name----", fieldName, iv, item, j, num)

	}
	return result
}

//FmtValue 将value转换为标准格式
func FmtValue(value interface{}) interface{} {
	var res interface{}
	if value != nil {
		typev := reflect.TypeOf(value)
		switch typev.Kind() {
		case reflect.Slice, reflect.Array:
			s := reflect.ValueOf(value)
			result := make([]interface{}, s.Len())
			for i := 0; i < s.Len(); i++ {

				item := s.Index(i)
				//typei := reflect.Type(item)
				if item.Type().Kind() == reflect.Struct {
					result[i] = fmtStruct(item)
				} else {
					//fmt.Println("")
					result[i] = item.Interface()
				}

			}
			res = result
		case reflect.Struct:
			item := reflect.ValueOf(value)
			res = fmtStruct(item)
		default:
			res = value
		}
	}
	return res
}
